package com.dcivision.dms.web;

import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.dcivision.dms.DmsErrorConstant;
import com.dcivision.dms.DmsOperationConstant;
import com.dcivision.dms.bean.DmsDocument;
import com.dcivision.dms.bean.DmsRoot;
import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.core.DocumentOperationManager;
import com.dcivision.dms.core.EmptyContentException;
import com.dcivision.dms.dao.DmsRootDAObject;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.PermissionManager;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.Utility;
import com.dcivision.framework.web.AbstractMaintAction;

public class MaintDmsCreateNewFolderAction extends AbstractMaintAction{
  public static final String REVISION = "$Revision: 1.9 $";

  public String getFunctionCode() {
    return null;
  }

  public String getMajorDAOClassName() {
    return null;
  }
  
  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException {
    MaintDmsCreateNewFolderForm actionForm = (MaintDmsCreateNewFolderForm) form;
    SessionContainer sessionContainer = this.getSessionContainer(request);
	  Connection conn = this.getConnection(request); 
	  DocumentOperationManager docOperationManager = new DocumentOperationManager(sessionContainer,conn);
    PermissionManager permissionManager = sessionContainer.getPermissionManager();
    String permission = permissionManager.getPermission(conn, GlobalConstant.OBJECT_TYPE_DOCUMENT, TextUtility.parseIntegerObj(actionForm.getParentID()));
    DmsRootDAObject dmsRootDAObject = new DmsRootDAObject(sessionContainer,conn);
    DmsRoot documentRoot = (DmsRoot)dmsRootDAObject.getObjectByID(TextUtility.parseIntegerObj(actionForm.getRootID()));
    
    
    try{
      if (!DmsOperationConstant.DMS_PERSONAL_ROOT.equals(documentRoot.getRootName())){
        if (permission.indexOf("F") < 0) {
          String actionTryToCarryOut = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "dms.label.permission_F");
          throw new ApplicationException(DmsErrorConstant.HAVE_NO_FUNCTION_PERMISSION,null,actionTryToCarryOut);
        }
      }
	 
	    if (GlobalConstant.NAV_MODE_NEW.equals(actionForm.getOpMode())){
		    DmsDocument dmsDocument = new DmsDocument();
		    dmsDocument.setDocumentName(actionForm.getDocumentName());
		    dmsDocument.setDocumentType(DmsDocument.FOLDER_TYPE);
		    dmsDocument.setParentID(TextUtility.parseIntegerObj(actionForm.getParentID()));
		    dmsDocument.setRootID(TextUtility.parseIntegerObj(actionForm.getRootID()));
		    dmsDocument.setCreateType("S");
		    dmsDocument.setUpdateCount(new Integer(0));
		    dmsDocument.setReferenceNo(actionForm.getReferenceNo());
		    dmsDocument.setDescription(actionForm.getDescription());
		    dmsDocument.setPriority(GlobalConstant.PRIORITY_MEDIUM);
        dmsDocument.setRecordStatus(GlobalConstant.STATUS_ACTIVE);
        dmsDocument.setItemStatus(DmsVersion.AVAILABLE_STATUS);
        if (Utility.isEmpty(dmsDocument.getDocumentName()) || "".equals(dmsDocument.getDocumentName().trim())){
          throw new EmptyContentException(DmsErrorConstant.EMPTY_FOLDER_NAME);
        }
        DmsDocument newDocument = docOperationManager.createFolder(dmsDocument);
        
        this.commit(request);
        request.setAttribute("doc",newDocument);
        addMessage(request, MESSAGE_RECORD_INSERT);
        actionForm.setDocumentName("");
        actionForm.setReferenceNo("");
        actionForm.setDescription("");
	    }
    } catch (ApplicationException appEx) {
      this.rollback(request);
      handleApplicationException(request, appEx);
	  }catch (Exception ioe) {
      log.error(ioe, ioe);      
    } finally {
      conn=null;
    }  	
	  return mapping.findForward(GlobalConstant.NAV_MODE_EDIT); 
  }
}
